package servlet;

import gameserver.SocketServer;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modle.Club;
import modle.Member;
import modle.MemberDAO;
import modle.Player;
import modle.PlayerInfo;
import modle.Table;

import com.google.gson.Gson;

/**
 * 在玩家首次进入游戏时，获得游戏的信息
 * @author Administrator
 * 
 */

public class GetGameInfoServlet extends HttpServlet {
	
	//public static final String NO_LOGIN = "访客";

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("getGameInfoServelt!!!!");
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		int roomIndex = Integer.parseInt(request.getParameter("selectedRoom"));
		int tableIndex = Integer.parseInt(request.getParameter("currentTable"));
		//int pos = Integer.parseInt(request.getParameter("pos"));
		
		SocketServer server = StartSocketServlet.socketServerList.get(roomIndex);
		
		
		System.out.println("或许游戏信息  tableindex:" + tableIndex);
		System.out.println("获取游戏信息  tableMap:" + server.tableMap);
		
		Table table = server.tableMap.get(tableIndex);
		
		if(table != null){
			String player1 = table.player1;
			String player2 = table.player2;
		}
		
		
		//如果需要更多的信息，就需要去数据库查询
		MemberDAO mdao = new MemberDAO();
		
//		HttpSession session = request.getSession();
//		String playername = (String) session.getAttribute("username");
		//判断客户端信息和服务器信息是否一致
		
		
		Gson gson = new Gson();
		
		//Object data[] = new Object[2];
		
		ArrayList gameInfo = new ArrayList();
		
		//存储最终发送给  客户端的数据
		ArrayList playersData = new ArrayList();
	
		
		for(Player player:table.players){
			PlayerInfo info = null;
			if(player != null)
				info = getPlayerInfo(player, mdao, table); 	//从数据库获得该用户的信息
			
			
			playersData.add(info); //如果该用户已退出该游戏，返回null
		}
		
		System.out.println("table.players 当前游戏中的人数:" + table.players);
		
		
		gameInfo.add(playersData); //添加玩家列表信息
		gameInfo.add("棋盘信息");//添加棋盘信息
		
		
		String result = gson.toJson(gameInfo);
		out.print(result);
		System.out.println("GetGameInfo:" + result);
		
		
		 response.setHeader("Pragma","No-cache");   
		  response.setHeader("Cache-Control","no-cache");   
		  response.setDateHeader("Expires", 0);  
		
		out.flush();
		out.close();
	}
	
	
	private PlayerInfo getPlayerInfo(Player p,MemberDAO mdao,Table table){
		//info 是最后要返回的单个用户的信息 
		PlayerInfo info = new PlayerInfo();
		
		String palyername = p.getName();
		
		//member是从数据库查询到的用户信息
		Member member = null;
		if(palyername != ""){ //不管是注册的还是未注册的用户，都存储在member表中
			
			info.state = p.state;
			
			//如果，玩家未登录 
			if(palyername.charAt(0) >= '0' && palyername.charAt(0) <= '9' && palyername.contains(".")){ //IP地址
//				member = new Member();
//				member.setMemberName("未登录用户");
				info.name = p.getName();
				info.location = "未知";
				info.type = "访客";
				
			}else{ //如果玩家以登录， 就去出查询相关的信息
				List list = mdao.findByMemberName(palyername);
				if(list.size() == 1)
					member = (Member) list.get(0);
				else
					System.out.println("获取信息失败");
				
				if(member != null){
					info.name = member.getMemberName();
					Set<Club> clubs = member.getClubs();
					
					
					info.type = "会员";
					info.clubs = new String[clubs.size()];
					info.location = member.getLocation(); 
					
					int i=0;
					//获得玩家加入的  俱乐部
					for(Club club:clubs){
						info.clubs[i++] = club.getClubLocalName();
					}
				}
				
			}
			System.out.println("游戏中获取信息：" + info.name);
			
			return info;
		}else
			return null;
		
	}
	

}
